package com.zrf.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zrf.dao.impl.BuyerDaoImpl;
import com.zrf.dao.impl.ProductDaoImpl;
import com.zrf.dao.impl.ShoppingcartDaoImpl;
import com.zrf.hibernate.model.PageBean;
import com.zrf.hibernate.pojo.Buyer;
import com.zrf.hibernate.pojo.Product;

@Service("productService")
public class ProductService {

	@Autowired
	private BuyerDaoImpl bdi = null;
	@Autowired
	private ProductDaoImpl pdi = null;
	@Autowired
	private ShoppingcartDaoImpl scdi = null;
	
	
	public Long getNumByProduct(){
		Long num = 0l;
		num = pdi.getAllNum();
		return num;
	}
	
	public List<Product> getAllProduct(PageBean pagebean){
		try {
			return pdi.findAll(pagebean).getResultList();
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		return null;
	}
	
	public boolean addShoppingCart(String name,Integer id,Integer num){
		try {
			if(num==0 || num==null){
				return false;
			}
			Buyer b = bdi.findByName(name);
			Product p = pdi.findById(id);
			return scdi.addShoppingCart(b, p, num);
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		return false;
	}
	
}
